package com.aijk.ehm.service;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.dao.mybatis.BasHealthAssessmentMapper;
import com.aijk.ehm.model.BasHealthAssessment;
import com.aijk.ehm.model.BasHealthAssessmentExample;
import com.aijk.ehm.model.qo.BasHealthAssessmentQo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 健康评估结果表
 */
@Service
public class BasHealthAssessmentService {
    private static Logger logger = LoggerFactory.getLogger(BasHealthAssessmentService.class);

    @Autowired
    private BasHealthAssessmentMapper mapper;

    public BasHealthAssessmentExample getExample(BasHealthAssessmentQo qo) {
        BasHealthAssessmentExample example = new BasHealthAssessmentExample();
        BasHealthAssessmentExample.Criteria criteria = example.createCriteria();
        if (qo != null) {
            if (Utils.isNotEmpty(qo.getSortway())) example.setOrderByClause(qo.getSortway());
            if (qo.getUid()!= null) criteria.andUidEqualTo(qo.getUid());
            if (qo.getQuestionnaireId()!= null) criteria.andQuestionnaireIdEqualTo(qo.getQuestionnaireId());
            if (qo.isStatus() == true) criteria.andQuestionnaireIdIsNotNull();
        }
        return example;
    }

    public int count(BasHealthAssessmentQo qo) {
        return mapper.countByExample(getExample(qo));
    }

    public int save(BasHealthAssessment bean) {
        return mapper.insertSelective(bean);
    }

    public int delete(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public int delete(BasHealthAssessmentQo qo) {
        return mapper.deleteByExample(getExample(qo));
    }

    public int update(BasHealthAssessment bean) {
        return mapper.updateByPrimaryKeySelective(bean);
    }

    public BasHealthAssessment findOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<BasHealthAssessment> find(BasHealthAssessmentQo qo) {
        return mapper.selectByExample(getExample(qo));
    }

    public PageInfo<BasHealthAssessment> page(PagingRequest pagingRequest, BasHealthAssessmentQo qo) {
        PageHelper.startPage(pagingRequest.getPageNum() , pagingRequest.getPageSize());
        return new PageInfo<BasHealthAssessment>(mapper.selectByExample(getExample(qo)));
    }
}